﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Iworld.Author.Service.Backstage.Import;
using Iworld.Author.Service.Backstage.Export;
using Iworld.basis.Service;
using Iworld.Main.Impl;
using Iworld.Author.Manager;
namespace Iworld.Author.Service.Backstage
{
    /// <summary>
    /// 后台用户服务
    /// </summary>
    public class AuthorService : IAuthorService
    {
        /// <summary>
        /// 获取提现记录
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public PageResult<WithdrawalsRecordExport> GetWithdrawalsRecordExports(GetWithdrawalsRecordExportsImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    return import.GetWithdrawalsRecordExports(db);
                }
            }
            catch (Exception ex)
            {
                return new PageResult<WithdrawalsRecordExport>(ex.Message);
            }
        }

        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public PageResult<AuthorExport> GetUsers(GetUsersImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    return import.GetUsers(db);
                }
            }
            catch (Exception ex)
            {
               return new PageResult<AuthorExport>(ex.Message);
            }
        }

        /// <summary>
        /// 获取登录记录
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public PageResult<AuthorLandingRecordExport> GetLandingRecords(GetLandingRecordsImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    return import.GetLandingRecords(db);
                }
            }
            catch (Exception ex)
            {
                return new PageResult<AuthorLandingRecordExport>(ex.Message);
            }
        }

        /// <summary>
        /// 获取帐变记录
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public PageResult<AuthorGoldsChangeRecordExport> GetGoldsChangeRecords(GetGoldsChangeRecordImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    return import.GetGoldsChangeRecords(db);
                }
            }
            catch (Exception ex)
            {
                return new PageResult<AuthorGoldsChangeRecordExport>(ex.Message);
            }
        }

        /// <summary>
        /// 获取充值卡记录
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public PageResult<RechargeCardRecordExport> GetRechargeCardRecords(GetRechargeCardRecordsImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    return import.GetRechargeCardRecords(db);
                }
            }
            catch (Exception ex)
            {
                return new PageResult<RechargeCardRecordExport>(ex.Message);
            }
        }



        /// <summary>
        /// 改变提现状态
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public NormalResult ChangeWithdrawalsStatus(ChangeWithdrawalsStatusImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    new WithdrawalsRecordManager(db).ChangeStatus(import);
                    return new NormalResult();
                }
            }
            catch (Exception ex)
            {
                return new NormalResult(ex.Message);
            }
        }

        /// <summary>
        /// 移除用户
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public NormalResult RemoveUser(RemoveUserImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    new AuthorManager(db).Remove(import);
                    return new NormalResult();
                }
            }
            catch (Exception ex)
            {
                return new NormalResult(ex.Message);
            }
        }

        /// <summary>
        /// 编辑用户
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public NormalResult EditUser(EditUserImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    new AuthorManager(db).Update(import);
                    return new NormalResult();
                }
            }
            catch (Exception ex)
            {
                return new NormalResult(ex.Message);
            }
        }

        /// <summary>
        /// 生成充值卡
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public NormalResult<RechargeCardRecordExport> GenerateRechargeCard(GenerateRechargeCardImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    RechargeCardRecordExport r = new RechargeCardRecordExport(new RechargeCardManager(db).Generate(import));
                    return new NormalResult<RechargeCardRecordExport>(r);
                }
            }
            catch (Exception ex)
            {
                return new NormalResult<RechargeCardRecordExport>(null,ex.Message);
            }
        }




    }
}
